package Parser;
/*
 * XMLToken.java
 *
 * Created on February 24, 2009, 1:09 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author Chris
 */
public class XMLToken {
    private String token;
    
    /** Creates a new instance of XMLToken */
    public XMLToken(String t) {
      token = t;
    }
    
    public boolean closes(XMLToken t) {
        if (isText() || t.isText())
          return false;
        return getName().equals(t.getName());
    }
    
    public boolean isSelfClosing() {
        if (token.length() < 2)
          return false;
        return token.charAt(token.length() - 2) == '/';
    }
    
    public String getName() {
        if (isText()) {
          return "";    
        }
        
        try{
        if (token.indexOf(' ') >= 0) {
            if (token.charAt(1) != '/')
                return token.substring(1, token.indexOf(' '));
            else
                return token.substring(2, token.indexOf(' '));
        } else {
            if (token.charAt(1) != '/') {
              if (token.charAt(token.length() - 2) != '/') {
                return token.substring(1, token.length() - 1);
              } else {
                return token.substring(1, token.length() - 2);
              }
            }
            else
                return token.substring(2, token.length() - 1);
        }
        } catch (Exception e) {
           // System.out.println(token);
            return "";
        }
    }
    
    public String toString() {
        return token;
    }
    
    public boolean isText() {
        if (token.charAt(0) == '<')
          return false;
        return true;
    }
    
    // Returns null if unspecified
    public String valueOfAtt(String att)
    {
        int pos;
        int pos2;
        String copy = token.toLowerCase();

        att = att.toLowerCase();

        // Find the attribute, and return its value
        pos = copy.indexOf(att);

        if (pos < 0)
          return null;

        // Find the equals sign
        pos = copy.indexOf('=', pos + 1);
        if (pos < 0)
            // No equal sign, fail
            return null;

        pos++;
        while (Character.isWhitespace(copy.charAt(pos)))
                pos++;

        if (copy.charAt(pos) == '"')
        {
            // It is, go to the next one
            pos2 = copy.indexOf('"', pos + 1);
            return token.substring(pos + 1, pos2);
        }
        else if (copy.charAt(pos) == '\'') {
            pos2 = copy.indexOf('\'', pos + 1);
            return token.substring(pos + 1, pos2);
        }
        else
        {
            pos2 = pos;
            while ( !Character.isWhitespace(copy.charAt(pos2)) && copy.charAt(pos2) != '>' )
            {
                pos2++;
            }

            return token.substring(pos, pos2);
        }
    }
    
}
